﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Text;
using System.Linq;
using System.IO;

namespace TileDownLoader.Elevation
{
    public class GPXLoader
    {
        /// <summary> 
        /// When passed a file, open it and parse all tracks 
        /// and track segments from it. 
        /// </summary> 
        /// <param name="sFile">Fully qualified file name (local)</param> 
        /// <returns>string containing line delimited waypoints from the 
        /// file (for test)</returns> 
        public gpx LoadGPXFile(Stream sFile)
        {
            XDocument gpxDoc = null;
            XNamespace gpxns = null;
            gpx gPx = null;
            try
            {
                gpxDoc = XDocument.Load(sFile);

                gpxns = XNamespace.Get("http://www.topografix.com/GPX/1/1"); ;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("gpx file load error:{0}", ex.Message), "Error", MessageBoxButton.OK);
            }

            try
            {
                if (gpxDoc != null && gpxns != null)
                {

                    gPx = new gpx();

                    gPx.creator = gpxDoc.Root.Attribute("creator") != null ? gpxDoc.Root.Attribute("creator").Value : "Home Brew";
                    gPx.version = gpxDoc.Root.Attribute("version") != null ? gpxDoc.Root.Attribute("version").Value : "1.0";


                    gPx.wpt = (from waypoint in gpxDoc.Descendants(gpxns + "wpt")
                               select new wpt
                               {
                                   lat = waypoint.Attribute("lat").Value,
                                   lon = waypoint.Attribute("lon").Value,
                                   ele = waypoint.Element(gpxns + "ele") != null ?
                                       waypoint.Element(gpxns + "ele").Value : null,
                                   name = waypoint.Element(gpxns + "name") != null ?
                                       waypoint.Element(gpxns + "name").Value : null,
                                   cmt = waypoint.Element(gpxns + "cmt") != null ?
                                       waypoint.Element(gpxns + "cmt").Value :null,
                                   sym = waypoint.Element(gpxns + "sym") != null ? waypoint.Element(gpxns + "sym").Value : null,
                                   time = waypoint.Element(gpxns + "time") != null ? DateTime.Parse(waypoint.Element(gpxns + "time").Value) : DateTime.Now
                               }).ToList<wpt>();


                    gPx.trk = (from track in gpxDoc.Descendants(gpxns + "trk")
                               select new trk
                               {
                                   name = track.Element(gpxns + "name") != null ? track.Element(gpxns + "name").Value : null,
                                   trkseg = (
                                        from trackpoint in track.Descendants(gpxns + "trkpt")
                                        select new trkpt
                                        {
                                            lat = trackpoint.Attribute("lat").Value,
                                            lon = trackpoint.Attribute("lon").Value,
                                            ele = trackpoint.Element(gpxns + "ele") != null ?
                                              trackpoint.Element(gpxns + "ele").Value : null,
                                            time = trackpoint.Element(gpxns + "time") != null ?
                                              DateTime.Parse(trackpoint.Element(gpxns + "time").Value) : DateTime.Now
                                        }
                                      ).ToList<trkpt>()
                               }).ToList<trk>();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("gpx file parsing error:{0}", ex.Message), "Error", MessageBoxButton.OK);
            }

            if (sFile != null)
            {
                sFile.Close();
                sFile.Dispose();
            }

            return gPx;
        }
    }
}
